﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GameLibrary.AI
{
    using Settlement;
    using Map;
    using Unit;
    public static class AIHelper
    {

        /// <summary>
        /// Adapt buiding problem into 0-1 knapsack problem.
        /// </summary>
        /// <param name="cities"></param>
        /// <param name="totalNumber"></param>
        /// <param name="budget"></param>
        /// <param name="profits"></param>
        /// <param name="weights"></param>
        /// <param name="?"></param>
        public static void ChangeSettlementBuildingPairIntoKnapsackInput(
            List<Settlement> settlements,
            int budget,
            out int totalNumber, // n
            out int capacity, // c
            out int[] profits, // pj
            out int[] weights // wj
            )
        {
            totalNumber = settlements.Count;
            capacity = budget;
            profits = new int[totalNumber]; 
            weights = new int[totalNumber];

            int i = 0;
            foreach(var v in settlements)
            {
                profits[i] = (int) v.Income;
                weights[i] = (int) v.GetNextLevelBuildingCost(SettlementHelper.Plant);
            }
        }

        /// <summary>
        /// Transfer unit-location pair into GKP input
        /// pij -- ceiling(10000 / movecost of unit i to position j)
        /// wij -- 1
        /// ci  -- position i needs how many units
        /// </summary>
        /// <param name="regions"></param>
        /// <param name="units"></param>
        /// <param name="n"></param>
        /// <param name="capacities"></param>
        /// <param name="profit"></param>
        /// <param name="weights"></param>
        /// <param name="?"></param>
        public static void ChangeUnitLocationPairIntoKnapsackInput(
            PlayerMap map,
            List<RegionInfo> regions,
            List<Unit> units,
            out int[,] profit, //pij
            out int[,] weights //wij
            )
        {
            int n = regions.Count;
            profit = new int[n, units.Count];
            weights = new int[n, units.Count];

            for (int i = 0; i < weights.GetLength(0); i++)
                for (int j = 0; j < weights.GetLength(1); j++)
                    weights[i, j] = 1;
                        
            for(int i=0;i<regions.Count;i++)
                for (int j = 0; j < units.Count; j++)
                {
                    double cost =
                        map.GetPath(
                            units[j].Location, 
                            regions[i].Start).TotalMoveCost;

                    cost = Math.Ceiling(10000 / cost);

                    profit[i, j] = (int)cost;
                }
        }
            
    }
}
